package com.hy.hongying.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hy.hongying.bo.VideLikesRedisKey;
import com.hy.hongying.bo.VideoHasLikesBo;
import com.hy.hongying.bo.VideoMakesLikesBo;
import com.hy.hongying.config.RedisConfig;
import com.hy.hongying.dao.VideoLikeDao;
import com.hy.hongying.entity.Videolike;
import com.hy.hongying.service.intf.VideoLikeService;
import com.hy.hongying.third.JedisUtil;
import com.hy.hongying.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

@Service
public class VideoLikeServiceImpl implements VideoLikeService {
    @Autowired
    private VideoLikeDao dao;
    /*
      视频点赞数
      已经查看对应用户是否点赞
      1、用户可能不用登录进行操作
      2、但是必需资源id
       */
    @Override
    public R videoHasLikesNums(Integer iid, Integer uid,Integer flag) {
        if(iid==0){
            return R.fail("数据异常");
        }
        VideoHasLikesBo videoHasLikesBo = new VideoHasLikesBo();
        videoHasLikesBo.setNums(dao.getResourcesNums(iid, flag));
        if (uid==null||uid==0){
            videoHasLikesBo.setFlag(0);
            return R.ok(videoHasLikesBo);
        }
        QueryWrapper<Videolike> videolikeQueryWrapper = new QueryWrapper<>();
        videolikeQueryWrapper.eq("iid",iid);
        videolikeQueryWrapper.eq("flag",flag);
        videolikeQueryWrapper.eq("uid",uid);
        List<Videolike> videolikes = dao.selectList(videolikeQueryWrapper);
        if (uid!=null &&uid>0){
            if (videolikes.size()>0){
                videoHasLikesBo.setFlag(1);
            }else {
                videoHasLikesBo.setFlag(2);
            }
        }
        System.err.println(videoHasLikesBo);
        return R.ok(videoHasLikesBo);
    }
    /*
    视频点赞
    flag=1点赞 2、没有点赞 0、用户没有登录
     */

    @Override
    public R videoMakesLikes(VideoMakesLikesBo bo, String token,Integer fg) {
        if (token!=null && token.length()==0){
            return R.fail("亲你还没有登录，赶紧去登陆吧");

        }

        String key= RedisConfig.VIDEO_LIKE+bo.getIid()+"="+bo.getUid()+"*"+bo.getFlag()+"%"+fg;

        /*
        如果点过赞就把她放在缓存中
         */
        if (!JedisUtil.checkKey(key)){
            //如果不存在就就把赞取消，反之依然
            List<Videolike> resourcesUser = dao.getResourcesUser(bo.getIid(),fg, bo.getUid());
            if (resourcesUser.size()>0){
                JedisUtil.addStr(key,"2",500);
            }else {
                JedisUtil.addStr(key,"1",500);
            }
        }else {
            //如果存在就根据vlues的值判断
            String values=JedisUtil.getStr(key);
            if (values.equals("1")){
                JedisUtil.addStr(key,"2",500);
            }else {
                JedisUtil.addStr(key,"1",500);

            }

        }
        return R.ok();
    }
    /*
    设置定时任务区
     */
    @Scheduled(fixedRate = 10000)//开启定时任务6秒钟执行一次
    private void tiMingTask(){
        Set<String> allKey = JedisUtil.getAllKey(RedisConfig.VIDEO_LIKE+"*");
        if (allKey.size()==0){
            //中断方法为0，避免空值
            return;
        }
        for (String str:
             allKey) {
            VideLikesRedisKey videLikesRedisKey = getVideLikesRedisKey(str);
            System.err.println(JedisUtil.getStr(str)+"======"+str);
            Videolike videolike = new Videolike(videLikesRedisKey.getIid(),videLikesRedisKey.getUid(),videLikesRedisKey.getFlag());
            if (videLikesRedisKey.getTypeFlag()==1&&JedisUtil.getStr(str).equals("2")){
                //删除数据库和缓存
                dao.delResourcesUser(videolike);
            }else if (videLikesRedisKey.getTypeFlag()!=1&&JedisUtil.getStr(str).equals("1")){
                System.err.println(videolike);
                dao.insert(videolike);
            }
            JedisUtil.delKey(str);
        }
    }
    /*
    解析键值
     */
    private VideLikesRedisKey getVideLikesRedisKey(String key){
        Integer iid=Integer.parseInt(key.substring(key.indexOf(":")+1,key.indexOf("=")));
        Integer uid=Integer.parseInt(key.substring(key.indexOf("=")+1,key.indexOf("*")));
        Integer typeFlag=Integer.parseInt(key.substring(key.indexOf("*")+1,key.indexOf("%")));
        Integer flag=Integer.parseInt(key.substring(key.length()-1,key.length()));
        return new VideLikesRedisKey(iid,uid,flag,typeFlag);
    }
}
